<?php
/**
 * Created by PhpStorm.
 * 楼小二订单绑定
 * User: caicm
 * Date: 2016/8/11
 * Time: 14:01
 */
namespace SDXApi\Model;
use Think\Model;

class LouxiaoerOrderModel extends Model
{
    /*
     * 绑定楼小二订单
     * Created By caicm
     * @param int $louxiaoer_user_id 楼小二用户id
     * @param int $user_id 用户id
     * @param int $address_id 地址id
     * @param int $landmark_id 地标id
     * @param int $order_id 订单id
     * @param int $louxiaoer_id 楼小二ID
     * @param int $binding_louxiaoer_time 绑定楼小二时间
     * @param string $created 操作者
     * return bool
     */
    public function orderBindingLouxiaoer($louxiaoer_user_id=0, $user_id, $address_id, $landmark_id, $order_id, $louxiaoer_id, $binding_louxiaoer_time=0, $created='', $is_first=0){

        $data = array(
            'louxiaoer_user_id' => $louxiaoer_user_id,
            'user_id' => $user_id,
            'address_id' => $address_id,
            'landmark_id' => $landmark_id,
            'order_id' => $order_id,
            'louxiaoer_id' => $louxiaoer_id,
            'add_time' => gmtime(),
            'binding_louxiaoer_time' => $binding_louxiaoer_time,
            'created' => $created,
            'is_first' => $is_first
        );

        return $this->add($data);

    }

    /*
     * 查询订单地址所属楼小二
     * Created By caicm
     * @param int $address_id
     * return mixed
     */
    public function findLouxiaoerByAddressId($address_id){
        $where = array(
            'lo.address_id' => $address_id,
            'lo.louxiaoer_id' => array('neq',0)
        );

        $field = "lo.landmark_id as id,lo.louxiaoer_id,w.name,a.user_name as phone,w.wechat_name,w.wechat_barcode_img";

        return   $this->alias('lo')
            ->field($field)
            ->join("left join sdx_warehouse_personnel as w on lo.louxiaoer_id=w.uid")
            ->join("left join sdx_admin_user as a on lo.louxiaoer_id=a.user_id")
            ->where($where)
            ->find();
    }

    /*
     * 查询订单所属地标楼小二
     * Created By caicm
     * @param int $order_id
     * return mixed
     */
    public function findOrderInLouxiaoerLandmark($order_id){

        $where = array(
            'o.order_id' => $order_id
        );

        $field = "o.sign_building,o.user_id,o.address_id,o.order_id,o.address,l.id,l.louxiaoer_id,r.id as region_id,r.wdtype,w.name,a.user_name as phone";

        return   M('order_info o')
            ->field($field)
            ->join("left join sdx_warehouse_deliver_region as r on o.wdr_id=r.id")
            ->join("left join sdx_louxiaoer_landmark l on o.sign_building=l.name and o.wdr_id=l.region_id ")
            ->join("left join sdx_warehouse_personnel as w on l.louxiaoer_id=w.uid")
            ->join("left join sdx_admin_user as a on l.louxiaoer_id=a.user_id")
            ->where($where)
            ->find();
    }

    /*
     * 查询楼小二订单
     * Created By caicm
     * @param int $order_id
     * return mixed
     */
    public function getLouxiaoerOrderByOrderId($order_id){

        $where = array(
            'order_id' => $order_id
        );

        return $this->where($where)->find();

    }

    /*
     * 统计楼小二订单数量
     */
    public function findCountOrderByLouxiaoerId($louxiaoer_id, $louxiaoer_user_id=0){

        $where = array(
            'louxiaoer_id' => $louxiaoer_id,
            'louxiaoer_user_id' => $louxiaoer_user_id
        );

        if($where['louxiaoer_user_id']==0) unset($where['louxiaoer_user_id']);

        return $this->where($where)->count();

    }

    /*
     * 统计楼小二订单数量预警总数
     */
    public function count_today_order($louxiaoer_id){

        $where = array(
            'lo.louxiaoer_id' => $louxiaoer_id,
            'a.state' => 0
        );

        return $this
                ->alias('lo')
                ->join("left join sdx_order_info o on lo.order_id=o.order_id")
                ->join("left join sdx_alarm_log2 a on o.order_sn=a.order_sn")
                ->where($where)
                ->count();

    }

    /*
     * 获取楼小二订单总数(当天)
     * @param int $louxiaoer_id
     * return number
     */
    public function count_check_order($louxiaoer_id){

        $where = array(
            'louxiaoer_id' => $louxiaoer_id,
            'add_time' => array('egt',strtotime(date("Y-m-d"))-28800)
        );

        return $this->where($where)->count();

    }

    /*
     * 订单列表
     * Created By caicm
     * @param int $louxiaoer_id
     * @param int $user_id
     * @param mixed $limit
     * return mixed
     */
    public function findOrderList($louxiaoer_id, $user_id, $limit){

        $where = array(
           // 'lo.louxiaoer_id' => $louxiaoer_id,
            'lo.user_id' => $user_id
        );

        $field = "o.order_id,o.order_sn as order_num,o.add_time as time,w.name,lo.louxiaoer_user_id";

        return $this
                ->alias('lo')
                ->field($field)
                ->join("left join sdx_order_info o on lo.order_id=o.order_id left join sdx_warehouse_personnel as w on lo.louxiaoer_id=w.uid")
                ->where($where)
                ->limit($limit)
                ->select();

    }

    /*
     * 订单列表
     * Created By caicm
     * @param int $louxiaoer_id
     * @param string $keyword 关键词（姓名、地址、订单号、手机)
     * @param $buy_type 0全部，1首单，2复单
     * @param $time_start 开始时间
     * @param $time_end 结束时间
     * @param mixed $limit
     * return mixed
     */
    public function get_order_list_with_date($louxiaoer_id, $keyword, $buy_type, $time_start, $time_end, $limit){

        $where['lo.louxiaoer_id'] = $louxiaoer_id ;
        if($buy_type == 1) $where['lo.is_first'] = 0 ;
        if($buy_type == 2) $where['lo.is_first'] = 1 ;

        if($time_start && $time_end){
            $where['o.add_time'] = array('between',array($time_start,$time_end)) ;
        }


        $field = "o.order_id,o.order_sn as order_num,o.add_time as time,lo.louxiaoer_user_id as member_id,o.consignee as member_name,
        o.mobile as phone,lo.is_first,
        concat(IFNULL(p.region_name, ''),IFNULL(t.region_name, ''),IFNULL(d.region_name, ''),IFNULL(o.address, '')) AS address";



        if($keyword){
            unset($where['lo.louxiaoer_id']);
            $where['_string'] = ' o.consignee like "%'.$keyword.'%"  OR  o.address like "%'.$keyword.'%" OR  o.mobile like "%'.$keyword.'%" OR  o.order_sn like "%'.$keyword.'%"  ';

            $data = M('order_info')
                    ->alias('o')
                    ->field($field)
                    ->join("left join sdx_louxiaoer_order lo on lo.order_id=o.order_id")
                    ->join('left join sdx_region p ON o.province = p.region_id')
                    ->join('left join sdx_region t ON o.city = t.region_id')
                    ->join('left join sdx_region d ON o.district = d.region_id')
                    ->where($where)
                    ->limit($limit)
                    ->select();

            return $data;
        }




        return $this
            ->alias('lo')
            ->field($field)
            ->join("left join sdx_order_info o on lo.order_id=o.order_id")
            ->join('left join sdx_region p ON o.province = p.region_id')
            ->join('left join sdx_region t ON o.city = t.region_id')
            ->join('left join sdx_region d ON o.district = d.region_id')
            ->where($where)
            ->limit($limit)
            ->select();

    }

    /*
     * 订单列表总数
     * Created By caicm
     * @param int $louxiaoer_id
     * @param string $keyword 关键词（姓名、地址、订单号、手机)
     * @param $buy_type 0全部，1首单，2复单
     * @param $time_start 开始时间
     * @param $time_end 结束时间
     * @param mixed $limit
     * return mixed
     */
    public function get_order_list_with_date_count($louxiaoer_id, $keyword, $buy_type, $time_start, $time_end){

        $where['lo.louxiaoer_id'] = $louxiaoer_id ;
        if($buy_type == 1) $where['lo.is_first'] = 1 ;
        if($buy_type == 2) $where['lo.is_first'] = 0 ;

        if($time_start && $time_end){
            $where['o.add_time'] = array('between',array($time_start,$time_end)) ;
        }


        if($keyword){
            unset($where['lo.louxiaoer_id']);
            $where['_string'] = ' o.consignee like "%'.$keyword.'%"  OR  o.address like "%'.$keyword.'%" OR  o.mobile like "%'.$keyword.'%" OR  o.order_sn like "%'.$keyword.'%"  ';
            return M('order_info')
                    ->alias('o')
                    ->join("left join sdx_louxiaoer_order lo on lo.order_id=o.order_id")
                    ->where($where)
                    ->count();
        }

        return $this
            ->alias('lo')
            ->join("left join sdx_order_info o on lo.order_id=o.order_id")
            ->where($where)
            ->count();

    }

    /*
     * 订单列表总数
     * Created By caicm
     * @param int $louxiaoer_id
     * @param int $user_id
     * return mixed
     */
    public function findOrderCount($louxiaoer_id, $user_id){

        $where = array(
            //'lo.louxiaoer_id' => $louxiaoer_id,
            'lo.user_id' => $user_id
        );

        return $this
            ->alias('lo')
            ->join("left join sdx_order_info o on lo.order_id=o.order_id")
            ->where($where)
            ->count();

    }

    /*
     * 获取订单详情
     * Created By caicm
     * @param int $louxiaoer_id
     * @param int $user_id
     * return mixed
     */
    public function findOrderInfo($order_id, $louxiaoer_id){
        $where = array(
            'lo.louxiaoer_id' => $louxiaoer_id,
            'lo.order_id' => $order_id
        );

        $field = "o.user_id,o.order_sn,lo.louxiaoer_id,lo.landmark_id,p.name as louxiaoer_name,o.order_status,o.pay_status,o.shipping_status";

        return $this
            ->alias('lo')
            ->field($field)
            ->join("left join sdx_order_info o on lo.order_id=o.order_id")
            ->join("left join sdx_warehouse_personnel p on lo.louxiaoer_id=p.uid")
            ->where($where)
            ->find();
    }

    /*
     * 获取订单楼小二信息
     * Created By caicm
     * @param int $order_id
     * return array
     */
    public function findOrderLouxiaoerInfoByOrderId($order_id){
        $where = array(
            'lo.order_id' => $order_id
        );

        $field = "p.name,p.phone,p.wechat_name,p.wechat_barcode_img";

        return $this
            ->alias('lo')
            ->field($field)
            ->join("left join sdx_warehouse_personnel p on lo.louxiaoer_id=p.uid")
            ->where($where)
            ->find();
    }



}